package com.oulu.ruokkis.activity;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.oulu.ruokkis.R;
import com.oulu.ruokkis.model.Restaurant;
import com.oulu.ruokkis.parser.RSSMenuContext;
import com.oulu.ruokkis.parser.impl.RSSMenuParserJuissi;

public class MainActivity extends Activity {

	private ExpandableListView mExpandableList;
	private MyCustomAdapter myAdapter;
	private RSSMenuContext rssMenu;
	private List<Restaurant> restaurants;
	private TextView textDate;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         if(isOnline() == false)
         {
			AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
			alertDialogBuilder.setTitle(R.string.error_en_internet_connection);

			// set dialog message
			alertDialogBuilder
					.setMessage(R.string.error_en_internet_connection_message)
					.setCancelable(false)
					.setPositiveButton("OK",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,	int id) {
									System.exit(-1);
								}
							});

			AlertDialog alertDialog = alertDialogBuilder.create();
			alertDialog.show();
         }
         else
         {
             textDate = (TextView) findViewById(R.id.textView1);
 			
             rssMenu = new RSSMenuContext();
             rssMenu.setParser(new RSSMenuParserJuissi(this, "http://webcgi.oulu.fi/opiskelijalle/ruokalistat_rss.php"));
             restaurants =  rssMenu.parseAll();
             
             mExpandableList = (ExpandableListView) findViewById(R.id.expandableListView1);
             myAdapter = new MyCustomAdapter(MainActivity.this, restaurants);
             mExpandableList.setAdapter(myAdapter);
             textDate.setText(restaurants.get(0).getDayOfWeek() +" "+ restaurants.get(0).getDate() );
         }
	}
	
	/**
	 * Checks if Internet connection is available
	 * @return
	 */
	public boolean isOnline() {
	    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
	    return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting();
	}
	

	// Android Menu button press. Settings / Refresh
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

//	@Override
//	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
//		menu.setHeaderTitle("Sample menu");
//		menu.add(0, 0, 0, R.string.expandable_list_sample_action);	
//	}

	public boolean onOptionsItemSelected(MenuItem item) {

		Log.d("MainActivity", "onOptionsItemSelected: " + item);

		switch (item.getItemId()) {
		case R.id.action_refresh:

			rssMenu.setParser(new RSSMenuParserJuissi(this, "http://webcgi.oulu.fi/opiskelijalle/ruokalistat_rss.php"));
			restaurants =  rssMenu.parseAll();
			myAdapter = new MyCustomAdapter(MainActivity.this, restaurants);
			mExpandableList.setAdapter(myAdapter);
            textDate.setText(restaurants.get(0).getDayOfWeek() +" "+ restaurants.get(0).getDate() );
			myAdapter.notifyDataSetInvalidated();
			myAdapter.notifyDataSetChanged();
			mExpandableList.invalidate();

			return true;
			
		// TODO Display settings Activity
//		case R.id.action_settings:
//			return true;
			
		default:
			return super.onOptionsItemSelected(item);
		}

	}
}

